#include "button_fun.h"
#include <Arduino.h>
#include <OneButton.h>
#include <nvs_flash.h>
#include "common_util.h"

OneButton* obts = (OneButton*)malloc(16 * sizeof(OneButton));

void buttonInit() {
  uint8_t key_count = 5;
  if (!disableSerial1) {
    key_count = 3;
  }

  for (uint8_t i = 0; i < key_count; i++) {
    nvs_get_u8(my_nvs_handle_t, obtk[i], &obtp[i]);
  }

  for (uint8_t i = 0; i < key_count; i++) {
    if (obtp[i] >= 99 || obtp[i] < 0) {
      continue;
    }
    obts[i] = OneButton(obtp[i]);
  }
  for (uint8_t i = 0; i < key_count; i++) {
    Serial.print("KEY_");
    Serial.print(i);
    Serial.print("_PIN:");
    Serial.print(obtp[i]);
    Serial.print("   SYS_K");
    Serial.print(i);
    Serial.print("0");
    Serial.println(i);
  }
  // 初始化按钮线程
  Serial.println("init button");
  if (obtp[0] < 99 && obtp[0] >= 0) {
    xTaskCreate(buttonTask0, "buttonTask0", 1024 * 5, NULL, 1, NULL);
    Serial.print("K1 OK!");
  }
  if (obtp[1] < 99 && obtp[1] >= 0) {
    xTaskCreate(buttonTask1, "buttonTask1", 1024 * 5, NULL, 1, NULL);
    Serial.print(" K2:OK!");
  }
  if (obtp[2] < 99 && obtp[2] >= 0) {
    xTaskCreate(buttonTask2, "buttonTask2", 1024 * 5, NULL, 1, NULL);
    Serial.print(" K3:OK!");
  }
  //如果没有启用串口1，则继续初始化k3和K4两个按钮
  if (disableSerial1) {
    if (obtp[3] < 99 && obtp[3] >= 0) {
      xTaskCreate(buttonTask3, "buttonTask3", 1024 * 5, NULL, 1, NULL);
      Serial.print(" K4:OK!");
    }
    if (obtp[4] < 99 && obtp[4] >= 0) {
      xTaskCreate(buttonTask4, "buttonTask4", 1024 * 5, NULL, 1, NULL);
      Serial.print(" K5:OK!");
    }
  }
  Serial.println();
}

void buttonTask0(void* ptParam) {
  obts[0].attachClick(click0);
  obts[0].attachDoubleClick(doubleclick0);
  obts[0].attachLongPressStart(longclick0);
  while (1) {
    obts[0].tick();
    delay(10);
  }
}

void buttonTask1(void* ptParam) {
  obts[1].attachClick(click1);
  obts[1].attachDoubleClick(doubleclick1);
  obts[1].attachLongPressStart(longclick1);
  while (1) {
    obts[1].tick();
    delay(10);
  }
}

void buttonTask2(void* ptParam) {
  obts[2].attachClick(click2);
  obts[2].attachDoubleClick(doubleclick2);
  obts[2].attachLongPressStart(longclick2);
  while (1) {
    obts[2].tick();
    delay(10);
  }
}

void buttonTask3(void* ptParam) {
  obts[3].attachClick(click3);
  obts[3].attachDoubleClick(doubleclick3);
  obts[3].attachLongPressStart(longclick3);
  while (1) {
    obts[3].tick();
    delay(10);
  }
}

void buttonTask4(void* ptParam) {
  obts[4].attachClick(click4);
  obts[4].attachDoubleClick(doubleclick4);
  obts[4].attachLongPressStart(longclick4);
  while (1) {
    obts[4].tick();
    delay(10);
  }
}

void click0() {
  bgKye(0);
  broadcast("K_0_C;");
}

void doubleclick0() {
  broadcast("K_0_D;");
}

void longclick0() {
  broadcast("K_0_L;");
}

void click1() {
  bgKye(1);
  broadcast("K_1_C;");
}

void doubleclick1() {
  broadcast("K_1_D;");
}

void longclick1() {
  broadcast("K_1_L;");
}

void click2() {
  bgKye(2);
  broadcast("K_2_C;");
}

void doubleclick2() {
  broadcast("K_2_D;");
}

void longclick2() {
  broadcast("K_2_L;");
}

void click3() {
  bgKye(3);
  broadcast("K_3_C;");
}

void doubleclick3() {
  broadcast("K_3_D;");
}

void longclick3() {
  broadcast("K_3_L;");
}

void click4() {
  bgKye(4);
  broadcast("K_4_C;");
}

void doubleclick4() {
  broadcast("K_4_D;");
}

void longclick4() {
  broadcast("K_4_L;");
}

// 响应按键切换灯光
void bgKye(uint8_t key) {
  // 如果按下的按键和背光控制绑定的按键边编号一样。且背光控制IO有效。
  if (key == lcdbgKeyIndex && lcdbgGio < 99) {
    // 切换背光两灭
    if (digitalRead(lcdbgGio)) {
      digitalWrite(lcdbgGio, LOW);
    } else {
      digitalWrite(lcdbgGio, HIGH);
    }
  }
}
